home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / utils.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  31KB  |  1,324 lines

  1. /* General utility routines for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/param.h>
  23. #include <pwd.h>
  24. #include <varargs.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27.  
  28. #include "defs.h"
  29. #include "param.h"
  30. #include "signals.h"
  31. #include "gdbcmd.h"
  32. #include "terminal.h"
  33. #include "bfd.h"
  34. #include "target.h"
  35.  
  36. extern volatile void return_to_top_level ();
  37. extern volatile void exit ();
  38. extern char *gdb_readline ();
  39. extern char *getenv();
  40. extern char *malloc();
  41. extern char *realloc();
  42.  
  43. /* If this definition isn't overridden by the header files, assume
  44.    that isatty and fileno exist on this system.  */
  45. #ifndef ISATTY
  46. #define ISATTY(FP)    (isatty (fileno (FP)))
  47. #endif
  48.  
  49. #ifdef MISSING_VPRINTF
  50. #ifdef __GNU_LIBRARY
  51. #undef MISSING_VPRINTF
  52. #else  /* !__GNU_LIBRARY */
  53.  
  54. #ifndef vfprintf
  55. /* Can't #define it since language.c needs it (though FIXME it shouldn't) */
  56. void
  57. vfprintf (file, format, ap)
  58.      FILE *file;
  59.      char *format;
  60.      va_list ap;
  61. {
  62.   _doprnt (format, ap, file);
  63. }
  64. #endif /* vfprintf */
  65.  
  66. #ifndef vprintf
  67. /* Can't #define it since printcmd.c needs it */
  68. void
  69. vprintf (format, ap)
  70.      char *format;
  71.      va_list ap;
  72. {
  73.   vfprintf (stdout, format, ap);
  74. }
  75. #endif /* vprintf */
  76.  
  77. #endif /* GNU_LIBRARY */
  78. #endif /* MISSING_VPRINTF */
  79.  
  80. void error ();
  81. void fatal ();
  82.  
  83. /* Chain of cleanup actions established with make_cleanup,
  84.    to be executed if an error happens.  */
  85.  
  86. static struct cleanup *cleanup_chain;
  87.  
  88. /* Nonzero means a quit has been requested.  */
  89.  
  90. int quit_flag;
  91.  
  92. /* Nonzero means quit immediately if Control-C is typed now,
  93.    rather than waiting until QUIT is executed.  */
  94.  
  95. int immediate_quit;
  96.  
  97. /* Nonzero means that encoded C++ names should be printed out in their
  98.    C++ form rather than raw.  */
  99.  
  100. int demangle = 1;
  101.  
  102. /* Nonzero means that encoded C++ names should be printed out in their
  103.    C++ form even in assembler language displays.  If this is set, but
  104.    DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls.  */
  105.  
  106. int asm_demangle = 0;
  107.  
  108. /* Nonzero means that strings with character values >0x7F should be printed
  109.    as octal escapes.  Zero means just print the value (e.g. it's an
  110.    international character, and the terminal or window can cope.)  */
  111.  
  112. int sevenbit_strings = 0;
  113.  
  114. /* String to be printed before error messages, if any.  */
  115.  
  116. char *error_pre_print;
  117.  
  118. /* Add a new cleanup to the cleanup_chain,
  119.    and return the previous chain pointer
  120.    to be passed later to do_cleanups or discard_cleanups.
  121.    Args are FUNCTION to clean up with, and ARG to pass to it.  */
  122.  
  123. struct cleanup *
  124. make_cleanup (function, arg)
  125.      void (*function) ();
  126.      int arg;
  127. {
  128.   register struct cleanup *new
  129.     = (struct cleanup *) xmalloc (sizeof (struct cleanup));
  130.   register struct cleanup *old_chain = cleanup_chain;
  131.  
  132.   new->next = cleanup_chain;
  133.   new->function = function;
  134.   new->arg = arg;
  135.   cleanup_chain = new;
  136.  
  137.   return old_chain;
  138. }
  139.  
  140. /* Discard cleanups and do the actions they describe
  141.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  142.  
  143. void
  144. do_cleanups (old_chain)
  145.      register struct cleanup *old_chain;
  146. {
  147.   register struct cleanup *ptr;
  148.   while ((ptr = cleanup_chain) != old_chain)
  149.     {
  150.       cleanup_chain = ptr->next;    /* Do this first incase recursion */
  151.       (*ptr->function) (ptr->arg);
  152.       free (ptr);
  153.     }
  154. }
  155.  
  156. /* Discard cleanups, not doing the actions they describe,
  157.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  158.  
  159. void
  160. discard_cleanups (old_chain)
  161.      register struct cleanup *old_chain;
  162. {
  163.   register struct cleanup *ptr;
  164.   while ((ptr = cleanup_chain) != old_chain)
  165.     {
  166.       cleanup_chain = ptr->next;
  167.       free (ptr);
  168.     }
  169. }
  170.  
  171. /* Set the cleanup_chain to 0, and return the old cleanup chain.  */
  172. struct cleanup *
  173. save_cleanups ()
  174. {
  175.   struct cleanup *old_chain = cleanup_chain;
  176.  
  177.   cleanup_chain = 0;
  178.   return old_chain;
  179. }
  180.  
  181. /* Restore the cleanup chain from a previously saved chain.  */
  182. void
  183. restore_cleanups (chain)
  184.      struct cleanup *chain;
  185. {
  186.   cleanup_chain = chain;
  187. }
  188.  
  189. /* This function is useful for cleanups.
  190.    Do
  191.  
  192.      foo = xmalloc (...);
  193.      old_chain = make_cleanup (free_current_contents, &foo);
  194.  
  195.    to arrange to free the object thus allocated.  */
  196.  
  197. void
  198. free_current_contents (location)
  199.      char **location;
  200. {
  201.   free (*location);
  202. }
  203.  
  204. /* Print an error message and return to command level.
  205.    The first argument STRING is the error message, used as a fprintf string,
  206.    and the remaining args are passed as arguments to it.  */
  207.  
  208. /* VARARGS */
  209. void
  210. error (va_alist)
  211.      va_dcl
  212. {
  213.   va_list args;
  214.   char *string;
  215.  
  216.   va_start (args);
  217.   target_terminal_ours ();
  218.   wrap_here("");            /* Force out any buffered output */
  219.   fflush (stdout);
  220.   if (error_pre_print)
  221.     fprintf (stderr, error_pre_print);
  222.   string = va_arg (args, char *);
  223.   vfprintf (stderr, string, args);
  224.   fprintf (stderr, "\n");
  225.   va_end (args);
  226.   return_to_top_level ();
  227. }
  228.  
  229. /* Print an error message and exit reporting failure.
  230.    This is for a error that we cannot continue from.
  231.    The arguments are printed a la printf.  */
  232.  
  233. /* VARARGS */
  234. void
  235. fatal (va_alist)
  236.      va_dcl
  237. {
  238.   va_list args;
  239.   char *string;
  240.  
  241.   va_start (args);
  242.   string = va_arg (args, char *);
  243.   fprintf (stderr, "gdb: ");
  244.   vfprintf (stderr, string, args);
  245.   fprintf (stderr, "\n");
  246.   va_end (args);
  247.   exit (1);
  248. }
  249.  
  250. /* Print an error message and exit, dumping core.
  251.    The arguments are printed a la printf ().  */
  252. /* VARARGS */
  253. void
  254. fatal_dump_core (va_alist)
  255.      va_dcl
  256. {
  257.   va_list args;
  258.   char *string;
  259.  
  260.   va_start (args);
  261.   string = va_arg (args, char *);
  262.   /* "internal error" is always correct, since GDB should never dump
  263.      core, no matter what the input.  */
  264.   fprintf (stderr, "gdb internal error: ");
  265.   vfprintf (stderr, string, args);
  266.   fprintf (stderr, "\n");
  267.   va_end (args);
  268.  
  269.   signal (SIGQUIT, SIG_DFL);
  270.   kill (getpid (), SIGQUIT);
  271.   /* We should never get here, but just in case...  */
  272.   exit (1);
  273. }
  274.  
  275. /* Memory management stuff (malloc friends).  */
  276.  
  277. #if defined (NO_MALLOC_CHECK)
  278. void
  279. init_malloc ()
  280. {}
  281. #else /* Have mcheck().  */
  282. static void
  283. malloc_botch ()
  284. {
  285.   fatal_dump_core ("Memory corruption");
  286. }
  287.  
  288. void
  289. init_malloc ()
  290. {
  291.   mcheck (malloc_botch);
  292.   mtrace ();
  293. }
  294. #endif /* Have mcheck().  */
  295.  
  296. /* Like malloc but get error if no storage available.  */
  297.  
  298. #ifdef __STDC__
  299. void *
  300. #else
  301. char *
  302. #endif
  303. xmalloc (size)
  304.      long size;
  305. {
  306.   register char *val;
  307.  
  308.   /* At least one place (dbxread.c:condense_misc_bunches where misc_count == 0)
  309.      GDB wants to allocate zero bytes.  */
  310.   if (size == 0)
  311.     return NULL;
  312.   
  313.   val = (char *) malloc (size);
  314.   if (!val)
  315.     fatal ("virtual memory exhausted.", 0);
  316.   return val;
  317. }
  318.  
  319. /* Like realloc but get error if no storage available.  */
  320.  
  321. #ifdef __STDC__
  322. void *
  323. #else
  324. char *
  325. #endif
  326. xrealloc (ptr, size)
  327.      char *ptr;
  328.      long size;
  329. {
  330.   register char *val = (char *) realloc (ptr, size);
  331.   if (!val)
  332.     fatal ("virtual memory exhausted.", 0);
  333.   return val;
  334. }
  335.  
  336. /* Print the system error message for errno, and also mention STRING
  337.    as the file name for which the error was encountered.
  338.    Then return to command level.  */
  339.  
  340. void
  341. perror_with_name (string)
  342.      char *string;
  343. {
  344.   extern int sys_nerr;
  345.   extern char *sys_errlist[];
  346.   char *err;
  347.   char *combined;
  348.  
  349.   if (errno < sys_nerr)
  350.     err = sys_errlist[errno];
  351.   else
  352.     err = "unknown error";
  353.  
  354.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  355.   strcpy (combined, string);
  356.   strcat (combined, ": ");
  357.   strcat (combined, err);
  358.  
  359.   /* I understand setting these is a matter of taste.  Still, some people
  360.      may clear errno but not know about bfd_error.  Doing this here is not
  361.      unreasonable. */
  362.   bfd_error = no_error;
  363.   errno = 0;
  364.  
  365.   error ("%s.", combined);
  366. }
  367.  
  368. /* Print the system error message for ERRCODE, and also mention STRING
  369.    as the file name for which the error was encountered.  */
  370.  
  371. void
  372. print_sys_errmsg (string, errcode)
  373.      char *string;
  374.      int errcode;
  375. {
  376.   extern int sys_nerr;
  377.   extern char *sys_errlist[];
  378.   char *err;
  379.   char *combined;
  380.  
  381.   if (errcode < sys_nerr)
  382.     err = sys_errlist[errcode];
  383.   else
  384.     err = "unknown error";
  385.  
  386.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  387.   strcpy (combined, string);
  388.   strcat (combined, ": ");
  389.   strcat (combined, err);
  390.  
  391.   printf ("%s.\n", combined);
  392. }
  393.  
  394. /* Control C eventually causes this to be called, at a convenient time.  */
  395.  
  396. void
  397. quit ()
  398. {
  399.   target_terminal_ours ();
  400.   wrap_here ((char *)0);        /* Force out any pending output */
  401. #ifdef HAVE_TERMIO
  402.   ioctl (fileno (stdout), TCFLSH, 1);
  403. #else /* not HAVE_TERMIO */
  404.   ioctl (fileno (stdout), TIOCFLUSH, 0);
  405. #endif /* not HAVE_TERMIO */
  406. #ifdef TIOCGPGRP
  407.   error ("Quit");
  408. #else
  409.   error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
  410. #endif /* TIOCGPGRP */
  411. }
  412.  
  413. /* Control C comes here */
  414.  
  415. void
  416. request_quit ()
  417. {
  418.   quit_flag = 1;
  419.  
  420. #ifdef USG
  421.   /* Restore the signal handler.  */
  422.   signal (SIGINT, request_quit);
  423. #endif
  424.  
  425.   if (immediate_quit)
  426.     quit ();
  427. }
  428.  
  429. /* My replacement for the read system call.
  430.    Used like `read' but keeps going if `read' returns too soon.  */
  431.  
  432. int
  433. myread (desc, addr, len)
  434.      int desc;
  435.      char *addr;
  436.      int len;
  437. {
  438.   register int val;
  439.   int orglen = len;
  440.  
  441.   while (len > 0)
  442.     {
  443.       val = read (desc, addr, len);
  444.       if (val < 0)
  445.     return val;
  446.       if (val == 0)
  447.     return orglen - len;
  448.       len -= val;
  449.       addr += val;
  450.     }
  451.   return orglen;
  452. }
  453.  
  454. /* Make a copy of the string at PTR with SIZE characters
  455.    (and add a null character at the end in the copy).
  456.    Uses malloc to get the space.  Returns the address of the copy.  */
  457.  
  458. char *
  459. savestring (ptr, size)
  460.      char *ptr;
  461.      int size;
  462. {
  463.   register char *p = (char *) xmalloc (size + 1);
  464.   bcopy (ptr, p, size);
  465.   p[size] = 0;
  466.   return p;
  467. }
  468.  
  469. /* The "const" is so it compiles under DGUX (which prototypes strsave
  470.    in <string.h>.  FIXME: This should be named "xstrsave", shouldn't it?
  471.    Doesn't real strsave return NULL if out of memory?  */
  472. char *
  473. strsave (ptr)
  474.      const char *ptr;
  475. {
  476.   return savestring (ptr, strlen (ptr));
  477. }
  478.  
  479. char *
  480. concat (s1, s2, s3)
  481.      char *s1, *s2, *s3;
  482. {
  483.   register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
  484.   register char *val = (char *) xmalloc (len);
  485.   strcpy (val, s1);
  486.   strcat (val, s2);
  487.   strcat (val, s3);
  488.   return val;
  489. }
  490.  
  491. void
  492. print_spaces (n, file)
  493.      register int n;
  494.      register FILE *file;
  495. {
  496.   while (n-- > 0)
  497.     fputc (' ', file);
  498. }
  499.  
  500. /* Ask user a y-or-n question and return 1 iff answer is yes.
  501.    Takes three args which are given to printf to print the question.
  502.    The first, a control string, should end in "? ".
  503.    It should not say how to answer, because we do that.  */
  504.  
  505. /* VARARGS */
  506. int
  507. query (va_alist)
  508.      va_dcl
  509. {
  510.   va_list args;
  511.   char *ctlstr;
  512.   register int answer;
  513.   register int ans2;
  514.  
  515.   /* Automatically answer "yes" if input is not from a terminal.  */
  516.   if (!input_from_terminal_p ())
  517.     return 1;
  518.  
  519.   while (1)
  520.     {
  521.       va_start (args);
  522.       ctlstr = va_arg (args, char *);
  523.       vfprintf (stdout, ctlstr, args);
  524.       va_end (args);
  525.       printf ("(y or n) ");
  526.       fflush (stdout);
  527.       answer = fgetc (stdin);
  528.       clearerr (stdin);        /* in case of C-d */
  529.       if (answer == EOF)    /* C-d */
  530.         return 1;
  531.       if (answer != '\n')    /* Eat rest of input line, to EOF or newline */
  532.     do 
  533.       {
  534.         ans2 = fgetc (stdin);
  535.         clearerr (stdin);
  536.       }
  537.         while (ans2 != EOF && ans2 != '\n');
  538.       if (answer >= 'a')
  539.     answer -= 040;
  540.       if (answer == 'Y')
  541.     return 1;
  542.       if (answer == 'N')
  543.     return 0;
  544.       printf ("Please answer y or n.\n");
  545.     }
  546. }
  547.  
  548. /* Parse a C escape sequence.  STRING_PTR points to a variable
  549.    containing a pointer to the string to parse.  That pointer
  550.    should point to the character after the \.  That pointer
  551.    is updated past the characters we use.  The value of the
  552.    escape sequence is returned.
  553.  
  554.    A negative value means the sequence \ newline was seen,
  555.    which is supposed to be equivalent to nothing at all.
  556.  
  557.    If \ is followed by a null character, we return a negative
  558.    value and leave the string pointer pointing at the null character.
  559.  
  560.    If \ is followed by 000, we return 0 and leave the string pointer
  561.    after the zeros.  A value of 0 does not mean end of string.  */
  562.  
  563. int
  564. parse_escape (string_ptr)
  565.      char **string_ptr;
  566. {
  567.   register int c = *(*string_ptr)++;
  568.   switch (c)
  569.     {
  570.     case 'a':
  571.       return 007;        /* Bell (alert) char */
  572.     case 'b':
  573.       return '\b';
  574.     case 'e':            /* Escape character */
  575.       return 033;
  576.     case 'f':
  577.       return '\f';
  578.     case 'n':
  579.       return '\n';
  580.     case 'r':
  581.       return '\r';
  582.     case 't':
  583.       return '\t';
  584.     case 'v':
  585.       return '\v';
  586.     case '\n':
  587.       return -2;
  588.     case 0:
  589.       (*string_ptr)--;
  590.       return 0;
  591.     case '^':
  592.       c = *(*string_ptr)++;
  593.       if (c == '\\')
  594.     c = parse_escape (string_ptr);
  595.       if (c == '?')
  596.     return 0177;
  597.       return (c & 0200) | (c & 037);
  598.       
  599.     case '0':
  600.     case '1':
  601.     case '2':
  602.     case '3':
  603.     case '4':
  604.     case '5':
  605.     case '6':
  606.     case '7':
  607.       {
  608.     register int i = c - '0';
  609.     register int count = 0;
  610.     while (++count < 3)
  611.       {
  612.         if ((c = *(*string_ptr)++) >= '0' && c <= '7')
  613.           {
  614.         i *= 8;
  615.         i += c - '0';
  616.           }
  617.         else
  618.           {
  619.         (*string_ptr)--;
  620.         break;
  621.           }
  622.       }
  623.     return i;
  624.       }
  625.     default:
  626.       return c;
  627.     }
  628. }
  629.  
  630. /* Print the character CH on STREAM as part of the contents
  631.    of a literal string whose delimiter is QUOTER.  */
  632.  
  633. void
  634. printchar (ch, stream, quoter)
  635.      unsigned char ch;
  636.      FILE *stream;
  637.      int quoter;
  638. {
  639.   register int c = ch;
  640.  
  641.   if (c < 040 || (sevenbit_strings && c >= 0177)) {
  642.     switch (c)
  643.       {
  644.       case '\n':
  645.     fputs_filtered ("\\n", stream);
  646.     break;
  647.       case '\b':
  648.     fputs_filtered ("\\b", stream);
  649.     break;
  650.       case '\t':
  651.     fputs_filtered ("\\t", stream);
  652.     break;
  653.       case '\f':
  654.     fputs_filtered ("\\f", stream);
  655.     break;
  656.       case '\r':
  657.     fputs_filtered ("\\r", stream);
  658.     break;
  659.       case '\033':
  660.     fputs_filtered ("\\e", stream);
  661.     break;
  662.       case '\007':
  663.     fputs_filtered ("\\a", stream);
  664.     break;
  665.       default:
  666.     fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
  667.     break;
  668.       }
  669.   } else {
  670.     if (c == '\\' || c == quoter)
  671.       fputs_filtered ("\\", stream);
  672.     fprintf_filtered (stream, "%c", c);
  673.   }
  674. }
  675.  
  676. /* Number of lines per page or UINT_MAX if paging is disabled.  */
  677. static unsigned int lines_per_page;
  678. /* Number of chars per line or UNIT_MAX is line folding is disabled.  */
  679. static unsigned int chars_per_line;
  680. /* Current count of lines printed on this page, chars on this line.  */
  681. static unsigned int lines_printed, chars_printed;
  682.  
  683. /* Buffer and start column of buffered text, for doing smarter word-
  684.    wrapping.  When someone calls wrap_here(), we start buffering output
  685.    that comes through fputs_filtered().  If we see a newline, we just
  686.    spit it out and forget about the wrap_here().  If we see another
  687.    wrap_here(), we spit it out and remember the newer one.  If we see
  688.    the end of the line, we spit out a newline, the indent, and then
  689.    the buffered output.
  690.  
  691.    wrap_column is the column number on the screen where wrap_buffer begins.
  692.      When wrap_column is zero, wrapping is not in effect.
  693.    wrap_buffer is malloc'd with chars_per_line+2 bytes. 
  694.      When wrap_buffer[0] is null, the buffer is empty.
  695.    wrap_pointer points into it at the next character to fill.
  696.    wrap_indent is the string that should be used as indentation if the
  697.      wrap occurs.  */
  698.  
  699. static char *wrap_buffer, *wrap_pointer, *wrap_indent;
  700. static int wrap_column;
  701.  
  702. /* ARGSUSED */
  703. static void 
  704. set_width_command (args, from_tty, c)
  705.      char *args;
  706.      int from_tty;
  707.      struct cmd_list_element *c;
  708. {
  709.   if (!wrap_buffer)
  710.     {
  711.       wrap_buffer = (char *) xmalloc (chars_per_line + 2);
  712.       wrap_buffer[0] = '\0';
  713.     }
  714.   else
  715.     wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
  716.   wrap_pointer = wrap_buffer;    /* Start it at the beginning */
  717. }
  718.  
  719. static void
  720. prompt_for_continue ()
  721. {
  722.   char *ignore;
  723.  
  724.   immediate_quit++;
  725.   ignore = gdb_readline ("---Type <return> to continue---");
  726.   if (ignore)
  727.     free (ignore);
  728.   chars_printed = lines_printed = 0;
  729.   immediate_quit--;
  730.   dont_repeat ();        /* Forget prev cmd -- CR won't repeat it. */
  731. }
  732.  
  733. /* Reinitialize filter; ie. tell it to reset to original values.  */
  734.  
  735. void
  736. reinitialize_more_filter ()
  737. {
  738.   lines_printed = 0;
  739.   chars_printed = 0;
  740. }
  741.  
  742. /* Indicate that if the next sequence of characters overflows the line,
  743.    a newline should be inserted here rather than when it hits the end. 
  744.    If INDENT is nonzero, it is a string to be printed to indent the
  745.    wrapped part on the next line.  INDENT must remain accessible until
  746.    the next call to wrap_here() or until a newline is printed through
  747.    fputs_filtered().
  748.  
  749.    If the line is already overfull, we immediately print a newline and
  750.    the indentation, and disable further wrapping.
  751.  
  752.    If we don't know the width of lines, but we know the page height,
  753.    we must not wrap words, but should still keep track of newlines
  754.    that were explicitly printed.
  755.  
  756.    INDENT should not contain tabs, as that
  757.    will mess up the char count on the next line.  FIXME.  */
  758.  
  759. void
  760. wrap_here(indent)
  761.   char *indent;
  762. {
  763.   if (wrap_buffer[0])
  764.     {
  765.       *wrap_pointer = '\0';
  766.       fputs (wrap_buffer, stdout);
  767.     }
  768.   wrap_pointer = wrap_buffer;
  769.   wrap_buffer[0] = '\0';
  770.   if (chars_per_line == UINT_MAX)        /* No line overflow checking */
  771.     {
  772.       wrap_column = 0;
  773.     }
  774.   else if (chars_printed >= chars_per_line)
  775.     {
  776.       puts_filtered ("\n");
  777.       puts_filtered (indent);
  778.       wrap_column = 0;
  779.     }
  780.   else
  781.     {
  782.       wrap_column = chars_printed;
  783.       wrap_indent = indent;
  784.     }
  785. }
  786.  
  787. /* Like fputs but pause after every screenful, and can wrap at points
  788.    other than the final character of a line.
  789.    Unlike fputs, fputs_filtered does not return a value.
  790.    It is OK for LINEBUFFER to be NULL, in which case just don't print
  791.    anything.
  792.  
  793.    Note that a longjmp to top level may occur in this routine
  794.    (since prompt_for_continue may do so) so this routine should not be
  795.    called when cleanups are not in place.  */
  796.  
  797. void
  798. fputs_filtered (linebuffer, stream)
  799.      char *linebuffer;
  800.      FILE *stream;
  801. {
  802.   char *lineptr;
  803.  
  804.   if (linebuffer == 0)
  805.     return;
  806.   
  807.   /* Don't do any filtering if it is disabled.  */
  808.   if (stream != stdout
  809.    || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
  810.     {
  811.       fputs (linebuffer, stream);
  812.       return;
  813.     }
  814.  
  815.   /* Go through and output each character.  Show line extension
  816.      when this is necessary; prompt user for new page when this is
  817.      necessary.  */
  818.   
  819.   lineptr = linebuffer;
  820.   while (*lineptr)
  821.     {
  822.       /* Possible new page.  */
  823.       if (lines_printed >= lines_per_page - 1)
  824.     prompt_for_continue ();
  825.  
  826.       while (*lineptr && *lineptr != '\n')
  827.     {
  828.       /* Print a single line.  */
  829.       if (*lineptr == '\t')
  830.         {
  831.           if (wrap_column)
  832.         *wrap_pointer++ = '\t';
  833.           else
  834.         putc ('\t', stream);
  835.           /* Shifting right by 3 produces the number of tab stops
  836.              we have already passed, and then adding one and
  837.          shifting left 3 advances to the next tab stop.  */
  838.           chars_printed = ((chars_printed >> 3) + 1) << 3;
  839.           lineptr++;
  840.         }
  841.       else
  842.         {
  843.           if (wrap_column)
  844.         *wrap_pointer++ = *lineptr;
  845.           else
  846.             putc (*lineptr, stream);
  847.           chars_printed++;
  848.           lineptr++;
  849.         }
  850.       
  851.       if (chars_printed >= chars_per_line)
  852.         {
  853.           unsigned int save_chars = chars_printed;
  854.  
  855.           chars_printed = 0;
  856.           lines_printed++;
  857.           /* If we aren't actually wrapping, don't output newline --
  858.          if chars_per_line is right, we probably just overflowed
  859.          anyway; if it's wrong, let us keep going.  */
  860.           if (wrap_column)
  861.         putc ('\n', stream);
  862.  
  863.           /* Possible new page.  */
  864.           if (lines_printed >= lines_per_page - 1)
  865.         prompt_for_continue ();
  866.  
  867.           /* Now output indentation and wrapped string */
  868.           if (wrap_column)
  869.         {
  870.           if (wrap_indent)
  871.             fputs (wrap_indent, stream);
  872.           *wrap_pointer = '\0';        /* Null-terminate saved stuff */
  873.           fputs (wrap_buffer, stream);    /* and eject it */
  874.           /* FIXME, this strlen is what prevents wrap_indent from
  875.              containing tabs.  However, if we recurse to print it
  876.              and count its chars, we risk trouble if wrap_indent is
  877.              longer than (the user settable) chars_per_line. 
  878.              Note also that this can set chars_printed > chars_per_line
  879.              if we are printing a long string.  */
  880.           chars_printed = strlen (wrap_indent)
  881.                 + (save_chars - wrap_column);
  882.           wrap_pointer = wrap_buffer;    /* Reset buffer */
  883.           wrap_buffer[0] = '\0';
  884.           wrap_column = 0;        /* And disable fancy wrap */
  885.          }
  886.         }
  887.     }
  888.  
  889.       if (*lineptr == '\n')
  890.     {
  891.       chars_printed = 0;
  892.       wrap_here ((char *)0);  /* Spit out chars, cancel further wraps */
  893.       lines_printed++;
  894.       putc ('\n', stream);
  895.       lineptr++;
  896.     }
  897.     }
  898. }
  899.  
  900.  
  901. /* fputs_demangled is a variant of fputs_filtered that
  902.    demangles g++ names.*/
  903.  
  904. void
  905. fputs_demangled (linebuffer, stream, arg_mode)
  906.      char *linebuffer;
  907.      FILE *stream;
  908.      int arg_mode;
  909. {
  910. #ifdef __STDC__
  911.   extern char *cplus_demangle (const char *, int);
  912. #else
  913.   extern char *cplus_demangle ();
  914. #endif
  915. #define SYMBOL_MAX 1024
  916.  
  917. #define SYMBOL_CHAR(c) (isascii(c) \
  918.   && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
  919.  
  920.   char buf[SYMBOL_MAX+1];
  921. # define SLOP 5        /* How much room to leave in buf */
  922.   char *p;
  923.  
  924.   if (linebuffer == NULL)
  925.     return;
  926.  
  927.   /* If user wants to see raw output, no problem.  */
  928.   if (!demangle) {
  929.     fputs_filtered (linebuffer, stream);
  930.     return;
  931.   }
  932.  
  933.   p = linebuffer;
  934.  
  935.   while ( *p != (char) 0 ) {
  936.     int i = 0;
  937.  
  938.     /* collect non-interesting characters into buf */
  939.     while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
  940.       buf[i++] = *p;
  941.       p++;
  942.     }
  943.     if (i > 0) {
  944.       /* output the non-interesting characters without demangling */
  945.       buf[i] = (char) 0;
  946.       fputs_filtered(buf, stream);
  947.       i = 0;  /* reset buf */
  948.     }
  949.  
  950.     /* and now the interesting characters */
  951.     while (i < SYMBOL_MAX
  952.      && *p != (char) 0
  953.      && SYMBOL_CHAR(*p)
  954.      && i < (int)sizeof(buf) - SLOP) {
  955.       buf[i++] = *p;
  956.       p++;
  957.     }
  958.     buf[i] = (char) 0;
  959.     if (i > 0) {
  960.       char * result;
  961.       
  962.       if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
  963.     fputs_filtered(result, stream);
  964.     free(result);
  965.       }
  966.       else {
  967.     fputs_filtered(buf, stream);
  968.       }
  969.     }
  970.   }
  971. }
  972.  
  973. /* Print a variable number of ARGS using format FORMAT.  If this
  974.    information is going to put the amount written (since the last call
  975.    to INITIALIZE_MORE_FILTER or the last page break) over the page size,
  976.    print out a pause message and do a gdb_readline to get the users
  977.    permision to continue.
  978.  
  979.    Unlike fprintf, this function does not return a value.
  980.  
  981.    We implement three variants, vfprintf (takes a vararg list and stream),
  982.    fprintf (takes a stream to write on), and printf (the usual).
  983.  
  984.    Note that this routine has a restriction that the length of the
  985.    final output line must be less than 255 characters *or* it must be
  986.    less than twice the size of the format string.  This is a very
  987.    arbitrary restriction, but it is an internal restriction, so I'll
  988.    put it in.  This means that the %s format specifier is almost
  989.    useless; unless the caller can GUARANTEE that the string is short
  990.    enough, fputs_filtered should be used instead.
  991.  
  992.    Note also that a longjmp to top level may occur in this routine
  993.    (since prompt_for_continue may do so) so this routine should not be
  994.    called when cleanups are not in place.  */
  995.  
  996. #if !defined(MISSING_VPRINTF) || defined (vsprintf)
  997. /* VARARGS */
  998. void
  999. vfprintf_filtered (stream, format, args)
  1000.      va_list args;
  1001. #else
  1002. void fprintf_filtered (stream, format, arg1, arg2, arg3, arg4, arg5, arg6)
  1003. #endif
  1004.      FILE *stream;
  1005.      char *format;
  1006. {
  1007.   static char *linebuffer = (char *) 0;
  1008.   static int line_size;
  1009.   int format_length;
  1010.  
  1011.   format_length = strlen (format);
  1012.  
  1013.   /* Allocated linebuffer for the first time.  */
  1014.   if (!linebuffer)
  1015.     {
  1016.       linebuffer = (char *) xmalloc (255);
  1017.       line_size = 255;
  1018.     }
  1019.  
  1020.   /* Reallocate buffer to a larger size if this is necessary.  */
  1021.   if (format_length * 2 > line_size)
  1022.     {
  1023.       line_size = format_length * 2;
  1024.  
  1025.       /* You don't have to copy.  */
  1026.       free (linebuffer);
  1027.       linebuffer = (char *) xmalloc (line_size);
  1028.     }
  1029.  
  1030.  
  1031.   /* This won't blow up if the restrictions described above are
  1032.      followed.   */
  1033. #if !defined(MISSING_VPRINTF) || defined (vsprintf)
  1034.   (void) vsprintf (linebuffer, format, args);
  1035. #else
  1036.   (void) sprintf (linebuffer, format, arg1, arg2, arg3, arg4, arg5, arg6);
  1037. #endif
  1038.  
  1039.   fputs_filtered (linebuffer, stream);
  1040. }
  1041.  
  1042. #if !defined(MISSING_VPRINTF) || defined (vsprintf)
  1043. /* VARARGS */
  1044. void
  1045. fprintf_filtered (va_alist)
  1046.      va_dcl
  1047. {
  1048.   va_list args;
  1049.   FILE *stream;
  1050.   char *format;
  1051.  
  1052.   va_start (args);
  1053.   stream = va_arg (args, FILE *);
  1054.   format = va_arg (args, char *);
  1055.  
  1056.   /* This won't blow up if the restrictions described above are
  1057.      followed.   */
  1058.   (void) vfprintf_filtered (stream, format, args);
  1059.   va_end (args);
  1060. }
  1061.  
  1062. /* VARARGS */
  1063. void
  1064. printf_filtered (va_alist)
  1065.      va_dcl
  1066. {
  1067.   va_list args;
  1068.   char *format;
  1069.  
  1070.   va_start (args);
  1071.   format = va_arg (args, char *);
  1072.  
  1073.   (void) vfprintf_filtered (stdout, format, args);
  1074.   va_end (args);
  1075. }
  1076. #else
  1077. void
  1078. printf_filtered (format, arg1, arg2, arg3, arg4, arg5, arg6)
  1079.      char *format;
  1080.      int arg1, arg2, arg3, arg4, arg5, arg6;
  1081. {
  1082.   fprintf_filtered (stdout, format, arg1, arg2, arg3, arg4, arg5, arg6);
  1083. }
  1084. #endif
  1085.  
  1086. /* Easy */
  1087.  
  1088. void
  1089. puts_filtered (string)
  1090.      char *string;
  1091. {
  1092.   fputs_filtered (string, stdout);
  1093. }
  1094.  
  1095. /* Return a pointer to N spaces and a null.  The pointer is good
  1096.    until the next call to here.  */
  1097. char *
  1098. n_spaces (n)
  1099.      int n;
  1100. {
  1101.   register char *t;
  1102.   static char *spaces;
  1103.   static int max_spaces;
  1104.  
  1105.   if (n > max_spaces)
  1106.     {
  1107.       if (spaces)
  1108.     free (spaces);
  1109.       spaces = malloc (n+1);
  1110.       for (t = spaces+n; t != spaces;)
  1111.     *--t = ' ';
  1112.       spaces[n] = '\0';
  1113.       max_spaces = n;
  1114.     }
  1115.  
  1116.   return spaces + max_spaces - n;
  1117. }
  1118.  
  1119. /* Print N spaces.  */
  1120. void
  1121. print_spaces_filtered (n, stream)
  1122.      int n;
  1123.      FILE *stream;
  1124. {
  1125.   fputs_filtered (n_spaces (n), stream);
  1126. }
  1127.  
  1128. /* C++ demangler stuff.  */
  1129. char *cplus_demangle ();
  1130.  
  1131. /* Print NAME on STREAM, demangling if necessary.  */
  1132. void
  1133. fprint_symbol (stream, name)
  1134.      FILE *stream;
  1135.      char *name;
  1136. {
  1137.   char *demangled;
  1138.   if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
  1139.     fputs_filtered (name, stream);
  1140.   else
  1141.     {
  1142.       fputs_filtered (demangled, stream);
  1143.       free (demangled);
  1144.     }
  1145. }
  1146.  
  1147. #if !defined (USG_UTILS)
  1148. #define USG_UTILS defined (USG)
  1149. #endif
  1150.  
  1151. #if USG_UTILS
  1152. bcopy (from, to, count)
  1153. char *from, *to;
  1154. {
  1155.     memcpy (to, from, count);
  1156. }
  1157.  
  1158. bcmp (from, to, count)
  1159. {
  1160.     return (memcmp (to, from, count));
  1161. }
  1162.  
  1163. bzero (to, count)
  1164. char *to;
  1165. {
  1166.     while (count--)
  1167.         *to++ = 0;
  1168. }
  1169.  
  1170. getwd (buf)
  1171. char *buf;
  1172. {
  1173.   getcwd (buf, MAXPATHLEN);
  1174. }
  1175.  
  1176. char *
  1177. index (s, c)
  1178.      char *s;
  1179. {
  1180.   char *strchr ();
  1181.   return strchr (s, c);
  1182. }
  1183.  
  1184. char *
  1185. rindex (s, c)
  1186.      char *s;
  1187. {
  1188.   char *strrchr ();
  1189.   return strrchr (s, c);
  1190. }
  1191. #endif /* USG_UTILS.  */
  1192.  
  1193. #if !defined (QUEUE_MISSING)
  1194. #define QUEUE_MISSING defined (USG)
  1195. #endif
  1196.  
  1197. #if QUEUE_MISSING
  1198. /* Queue routines */
  1199.  
  1200. struct queue {
  1201.     struct queue *forw;
  1202.     struct queue *back;
  1203. };
  1204.  
  1205. insque (item, after)
  1206. struct queue *item;
  1207. struct queue *after;
  1208. {
  1209.     item->forw = after->forw;
  1210.     after->forw->back = item;
  1211.  
  1212.     item->back = after;
  1213.     after->forw = item;
  1214. }
  1215.  
  1216. remque (item)
  1217. struct queue *item;
  1218. {
  1219.     item->forw->back = item->back;
  1220.     item->back->forw = item->forw;
  1221. }
  1222. #endif /* QUEUE_MISSING */
  1223.  
  1224. #ifndef HAVE_STRSTR
  1225. /* Simple implementation of strstr, since some implementations lack it. */
  1226. const char *
  1227. strstr (in, find)
  1228.      const char *in, *find;
  1229. {
  1230.   register const char *p = in - 1;
  1231.  
  1232.   while (0 != (p = strchr (p+1, *find))) {
  1233.     if (strcmp (p, find))
  1234.       return p;
  1235.   }
  1236.   return 0;
  1237. }
  1238. #endif /* do not HAVE_STRSTR */
  1239.  
  1240. void
  1241. _initialize_utils ()
  1242. {
  1243.   struct cmd_list_element *c;
  1244.  
  1245.   c = add_set_cmd ("width", class_support, var_uinteger, 
  1246.           (char *)&chars_per_line,
  1247.           "Set number of characters gdb thinks are in a line.",
  1248.           &setlist);
  1249.   add_show_from_set (c, &showlist);
  1250.   c->function = set_width_command;
  1251.  
  1252.   add_show_from_set
  1253.     (add_set_cmd ("height", class_support,
  1254.           var_uinteger, (char *)&lines_per_page,
  1255.           "Set number of lines gdb thinks are in a page.", &setlist),
  1256.      &showlist);
  1257.   
  1258.   /* These defaults will be used if we are unable to get the correct
  1259.      values from termcap.  */
  1260.   lines_per_page = 24;
  1261.   chars_per_line = 80;
  1262.   /* Initialize the screen height and width from termcap.  */
  1263.   {
  1264.     char *termtype = getenv ("TERM");
  1265.  
  1266.     /* Positive means success, nonpositive means failure.  */
  1267.     int status;
  1268.  
  1269.     /* 2048 is large enough for all known terminals, according to the
  1270.        GNU termcap manual.  */
  1271.     char term_buffer[2048];
  1272.  
  1273.     if (termtype)
  1274.       {
  1275.     status = tgetent (term_buffer, termtype);
  1276.     if (status > 0)
  1277.       {
  1278.         int val;
  1279.         
  1280.         val = tgetnum ("li");
  1281.         if (val >= 0)
  1282.           lines_per_page = val;
  1283.         else
  1284.           /* The number of lines per page is not mentioned
  1285.          in the terminal description.  This probably means
  1286.          that paging is not useful (e.g. emacs shell window),
  1287.          so disable paging.  */
  1288.           lines_per_page = UINT_MAX;
  1289.         
  1290.         val = tgetnum ("co");
  1291.         if (val >= 0)
  1292.           chars_per_line = val;
  1293.       }
  1294.       }
  1295.   }
  1296.  
  1297.   /* If the output is not a terminal, don't paginate it.  */
  1298.   if (!ISATTY (stdout))
  1299.     lines_per_page = UINT_MAX;
  1300.  
  1301.   set_width_command ((char *)NULL, 0, c);
  1302.  
  1303.   add_show_from_set
  1304.     (add_set_cmd ("demangle", class_support, var_boolean, 
  1305.           (char *)&demangle,
  1306.         "Set demangling of encoded C++ names when displaying symbols.",
  1307.           &setprintlist),
  1308.      &showprintlist);
  1309.  
  1310.   add_show_from_set
  1311.     (add_set_cmd ("sevenbit-strings", class_support, var_boolean, 
  1312.           (char *)&sevenbit_strings,
  1313.    "Set printing of 8-bit characters in strings as \\nnn.",
  1314.           &setprintlist),
  1315.      &showprintlist);
  1316.  
  1317.   add_show_from_set
  1318.     (add_set_cmd ("asm-demangle", class_support, var_boolean, 
  1319.           (char *)&asm_demangle,
  1320.     "Set demangling of C++ names in disassembly listings.",
  1321.           &setprintlist),
  1322.      &showprintlist);
  1323. }
  1324.